<?
require_once("lib/php/controller/ViewController.php");
require_once("ericdingle/php/Authentication.php");
require_once("ericdingle/model/post/PostProvider.php");
require_once("ericdingle/model/label/LabelProvider.php");
require_once("ericdingle/model/link/LinkProvider.php");

class ViewPosts extends ViewController {
	public function doGet() {
		// get authentication
		$auth = Authentication::getInstance();
		
		// get params
		$labelID = $_GET["labelID"];
		$keyword = $_GET["keyword"];
		$page = $_GET["page"];

		if ($page === null) {
			$page = 1;
		}
		
		// get labels
		$labelProvider = LabelProvider::getInstance();
		$labels = $labelProvider->read();
		
		// get links
		$linkProvider = LinkProvider::getInstance();
		$links = $linkProvider->read();
		
		// get posts
		$postProvider = PostProvider::getInstance();
		if ($labelID) {
			$posts = $postProvider->readPageByLabelID($page, $labelID);
			$pageCount = $postProvider->readPageCountByLabelID($labelID);
		} else if ($keyword) {
			$posts = $postProvider->readPageByKeyword($page, $keyword);
			$pageCount = $postProvider->readPageCountByKeyword($keyword);
		} else {
			$posts = $postProvider->readPage($page);
			$pageCount = $postProvider->readPageCount();
		}
		
		// page url
		$pageUrl = "viewPosts?" .
			($labelID ? "labelID=" . $labelID . "&" : "") .
			($keyword ? "keyword=" . $keyword . "&" : "") .
			"page=";
		
		// load views
		require_once("ericdingle/php/Tabs.php");
		$this->loadView("ericdingle/view/header.php", array(
			"isLoggedIn" => $auth->isLoggedIn(),
			"tabID" => TabID::BLOG,
			"tabs" => $tabs,
			"css_files" => array("css/viewPosts.css", "css/blogNav.css", "css/post.css")
		));
		$this->loadView("ericdingle/view/viewPosts.php", array(
			"isLoggedIn" => $auth->isLoggedIn(),
			"labels" => $labels,
			"labelID" => $labelID,
			"keyword" => $keyword,
			"links" => $links,
			"posts" => $posts,
			"pageUrl" => $pageUrl,
			"page" => $page,
			"pageCount" => $pageCount
		));
		$this->loadView("ericdingle/view/footer.php");
	}
}
?>
